package sys.news.dao.impl;


import sys.news.dao.TopicDao;
import sys.news.pojo.Topic;
import sys.news.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TopicDaoImpl implements TopicDao {
    /**
     * 添加主题
     *
     * @param topic
     * @return
     */
    @Override
    public int addTopic(Topic topic) throws SQLException {
        String sql="INSERT INTO topic(tname) VALUE (?);";
        Object[] params = {topic.getTname() };
        return DBUtil.update(sql,params);
    }

    /**
     * 删除主题
     *
     * @param tid
     * @return
     */
    @Override
    public int delTopic(int tid) {
        String sql= "DELETE FROM topic WHERE tid = ?;";
        Object[] params = new Object[]{tid};
        return DBUtil.update(sql,params);
    }

    /**
     * 修改主题
     *
     * @param topic
     * @return
     */
    @Override
    public int updateTopic(Topic topic) {
        String sql = "UPDATE topic SET tname = ? WHERE tid = ?;";
        Object[] params = new Object[]{
               topic.getTname(),topic.getTid()
        };
        return DBUtil.update(sql,params);
    }

    /**
     * 通过 主题名字查询主题
     *
     * @param tname
     * @return
     */
    @Override
    public Topic getTopicByName(String tname) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Topic topic = null;
        try{
            //1.获取连接对象
            conn = DBUtil.getConnection();
            //2.执行sql
            String sql = " SELECT * FROM topic WHERE tname = ?;";
            System.out.println(sql);
            //3.获取预处理对象
            pstmt = conn.prepareStatement(sql);
            //4.传递参数
            pstmt.setString(1,tname);
            //5.处理结果集
            rs = pstmt.executeQuery();
            while(rs.next()){
                topic = new Topic();
                topic.setTid(rs.getInt("tid"));
                topic.setTname(rs.getString("tname"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.closeAll(conn, pstmt, rs);
        }
        return topic;

    }

    /**
     * 查询所有的主题
     *
     * @return
     */
    @Override
    public List<Topic> getAll() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
       List<Topic> list = new ArrayList<Topic>();
        try{
            //1.获取连接对象
            conn = DBUtil.getConnection();
            //2.执行sql
            String sql = " SELECT tid,tname FROM topic;";
            System.out.println(sql);
            //3.获取预处理对象
            pstmt = conn.prepareStatement(sql);
            //5.处理结果集

            while(rs.next()){
                Topic topic = new Topic();
                topic.setTid(rs.getInt("tid"));
                topic.setTname(rs.getString("tname"));
                list.add(topic);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.closeAll(conn, pstmt, rs);
        }
        return list;

    }
}
